package com.kakavr.user.service;

import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.math.NumberUtils;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Maps;
import com.kakavr.alipay.service.IAlipayService;
import com.kakavr.base.service.BaseService;
import com.kakavr.page.Pagination;
import com.kakavr.page.constant.PaginationConstant;
import com.kakavr.pay.util.WXPayUtil;
import com.kakavr.shop.service.ITradeFlowService;
import com.kakavr.user.dao.IUserRechargeDao;
import com.kakavr.work.service.IWorkRecordService;
import com.kakavr.wxpay.service.IWXPayService;
@Service
public class UserRechargeService extends BaseService implements
		IUserRechargeService {
	@Resource
	private IUserRechargeDao userRechargeDao;
	@Resource
	private IUserService userService;
	@Resource
	private IWXPayService wXPayService;
	@Resource
	private IAlipayService alipayService;
	@Resource
	private ITradeFlowService tradeFlowService;
	@Resource
	private IWorkRecordService workRecordService;
	
	private void addUserRecharge(Map<String, Object> map){
		map.put("createTime", DateTime.now().toDate());
		map.put("tradeno", WXPayUtil.generateUUID());
		Map<String, Object> work = workRecordService.selectStoreMaxWork(map);
		if(null != work && !work.isEmpty()){
			map.put("workId", work.get("id"));
		}
		userRechargeDao.insertUserRecharge(map);
	}

	@Override
	public String addUserRechargeByAlipay(Map<String, Object> map) {
		this.addUserRecharge(map);
		return alipayService.addAlipayLog(map);
	}

	@Override
	public String addUserRechargeByWXpay(Map<String, Object> map) {
		this.addUserRecharge(map);
		return wXPayService.addWXpayLog(map);
	}

	@Override
	public void updateUserRechargeByPay(Object userId,String tradeno,Object payment,Object disamount) {
		Map<String, Object> map = Maps.newHashMap();
		map.put("userId", userId);
		map.put("tradeno", tradeno);
		Map<String, Object> recharge = userRechargeDao.selectRechargeInfo(map);
		if(null == recharge || recharge.isEmpty()){
			return ;
		}
		int status = (int) recharge.get("status");
		if(status < 1){
			map.put("payment", payment);
			map.put("disamount", disamount);
			map.put("status", 1);
			userRechargeDao.updateUserRecharge(map);
			Map<String,Object> user = Maps.newHashMap();
			user.put("id", recharge.get("userId"));
			user.put("times", recharge.get("totalTimes"));
			user.put("mode", recharge.get("mode"));
			userService.updateUser(user);
			//添加店铺交易流水
			Map<String,Object> trade = Maps.newHashMap();
			trade.put("storeId", recharge.get("storeId"));
			trade.put("amount", payment);
			trade.put("category", 0);
			trade.put("userId", recharge.get("userId"));
			trade.put("rechargeId", recharge.get("id"));
			trade.put("mode", recharge.get("mode"));
			trade.put("type", 0);
			tradeFlowService.addTradeFlow(trade);
		}
	}

	@Override
	public Pagination<Map<String, Object>> selectRechargeByPage(
			Map<String, Object> map) {
		PageHelper.startPage(NumberUtils.toInt((String) map
				.get(PaginationConstant.PAGE_NO_NAME)), NumberUtils
				.toInt((String) map.get(PaginationConstant.PAGE_SIZE_NAME)));
		return new Pagination<Map<String,Object>>(userRechargeDao.selectRechargeList(map));
	}

	@Override
	public Pagination<Map<String, Object>> statisticsShopTotalCount(
			Map<String, Object> map) {
		PageHelper.startPage(NumberUtils.toInt((String) map
				.get(PaginationConstant.PAGE_NO_NAME)), NumberUtils
				.toInt((String) map.get(PaginationConstant.PAGE_SIZE_NAME)));
		return new Pagination<Map<String,Object>>(userRechargeDao.statisticsShopTotalCount(map));
	}

}
